package com.swjd.utpchat;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class ReveiceSocket implements Runnable {
	private DatagramSocket socket;

	public ReveiceSocket(DatagramSocket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		byte[] bys = new byte[1024];
		DatagramPacket packet = new DatagramPacket(bys, bys.length);
		while (true) {
			try {
				// 接箱
				socket.receive(packet);
				// 获取箱数据
				byte[] data = packet.getData();
				int length = packet.getLength();
				// 拼接字符串，从0到总长度
				String s = new String(data, 0, length);
				System.out.println(packet.getAddress() + ",发送过来的数据为：" + s);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
